<?php
session_start();
if (!isset($_SESSION['user']) || isset($_SESSION['admin']))
    header('location:index.php');
if (!isset($_SESSION['cart']))
    header('location:cart.php');
$ok = 0;
foreach ($_SESSION['cart'] as $k => $v) {
    if (isset($_SESSION['cart'][$k]) && $v > 0) {
        $ok = 1;
        break;
    }
}
if ($ok == 0)
    header('location: index.php');
?>

<!DOCTYPE html>
<html>
    <head>
        <title>3D|didongde 3D - Gui gio hang</title>
        <?php include ('modules/page_head.php'); ?>
        <style type="text/css">
            table {
                margin: 10px 0 30px 0;
            }

            table tr th, table tr td {
                background: #CCC;
                color: #000;
                border-bottom: thick #000;
                padding: 7px 4px;
                text-align: left;
            }

            table tr td {
                background: #F7F7F7;
                color: #111;
                height: 15px;
                border : 1px solid #8E8E8E;
            }
        </style>
    </head>

    <body>
        <div id="main">
            <div id="header">
                <div id="logo">
                    <h1><a href="index.php">Didongde</a></h1>
                    <div class="slogan">Where you can find your own best suitable mobile</div>
                    <?php include_once('modules/logo_header.php'); ?>
                </div>
                <div id="menubar">
                    <?php include_once('modules/menubar.php'); ?>
                </div>
            </div>
            <div id="site_content">
                <div id="sidebar_container">
                    <h2>Tìm kiếm nhanh</h2>
                    <div class="sidebar">
                        <!-- insert your sidebar items here -->
                        <?php
                        include ("modules/quick_search.php");
                        show_manu();
                        ?>
                    </div>
                    <div class="sidebar">
                        <?php show_price(); ?>
                    </div>
                </div>
                <div id="content">
                    <?php
                    include_once("modules/connection.php");
                    $conn = connectDB();
                    foreach ($_SESSION['cart'] as $key => $value) {
                        $item[] = $key;
                    }
                    $mail_sql = "select * from customer where username like'" . $_SESSION['user'] . "'";
                    $mail_prepared = $conn->prepare($mail_sql);
                    $mail_prepared->execute();
                    $mail_result = $mail_prepared->fetch();
                    $customerid = $mail_result['customerid'];
                    echo "<table>";
                    echo "<tr><th>Model</th><th>Số lượng</th><th>Giá tiền</th><th>Thành tiền</th></tr>";
                    $total = 0;
                    foreach ($item as $key) {
                        $sql = "select * from mobilephone
					where mobileid like '$key'
					order by mobileid asc";
                        $result = $conn->prepare($sql);
                        $result->execute();

                        while ($row = $result->fetch()) {
                            echo "<tr><td>" . $row['model'] . "</td>";
                            echo "<td style=\"text-align:right;\">" . $_SESSION['cart'][$row['mobileid']] . "</td>";
                            echo "<td>" . number_format($row['price'], 3, ',', ',') . " VNĐ</td>";
                            echo "<td>" . number_format($_SESSION['cart'][$row['mobileid']] * $row['price'], 3, ',', ',') . " VNĐ</td></tr>";
                            $qty_change_sql = "UPDATE mobilephone SET quantity = (" . $row['quantity'] . "-" . $_SESSION['cart'][$row['mobileid']] . ")
							WHERE mobileid like'" . $row['mobileid'] . "'";
                            //echo $number_change_sql;
                            $qty_change = $conn->prepare($qty_change_sql);
                            $qty_change->execute();
                            include_once('conf/dbconfig.php');
                            $cfg = new dbconfig();
                            $host = $cfg->get_dbhost();
                            $db = $cfg->get_dbname();
                            $usr = $cfg->get_dbusername();
                            $pass = $cfg->get_dbpwd();
                            $connection = pg_connect("host = $host dbname = $db user = $usr password = $pass")
                                    or die("Cannot connect to database server: " . pg_last_error());
                            $query_order = "SELECT * from orders";
                            $ordernumber = pg_num_rows(pg_query($connection, $query_order));
                            $orderid = strval($ordernumber + 1);
                            while (strlen($orderid) < 4)
                                $orderid = "0" . $orderid;
                            $date = date("Y-m-d");
                            $order_sql = "INSERT INTO orders(orderid,customerid,mobileid,quantity,date) VALUES
											('$orderid','$customerid','" . $row['mobileid'] . "'," . $_SESSION['cart'][$row['mobileid']] . ",'$date')";
                            $check = pg_query($connection, $order_sql);                            
                            /* $order_insert = $conn->prepare($order_sql);
                              $order_insert->execute();
                             */
                            $total += $_SESSION['cart'][$row['mobileid']] * $row['price'];
                        }
                    }
                    unset($_SESSION['cart']);

                    echo "<tr><td colspan=2 >Tổng giá trị đơn hàng: " . number_format($total, 3, ',', ',') . " VNĐ</td></tr>";
                    echo "</table>";
                    echo "<h5>Người mua: " . $_SESSION['user'] . "<br>";
                    echo "Thời gian mua: Ngày " . date("d - m - Y ") . " lúc " . date(" h:i:s A") . "<br/></h5>";
                    echo "<h4>Xin cảm ơn quý khách đã mua hàng!</h4>";
                    ?>
                </div>
            </div>
        </div>
        <div id="footer">
            <?php include('modules/footer.php'); ?>
        </div>
    </body>
</html>